package com.jy.shorturl.dao;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface ShortUrlDao {
	/**
	 * 插入全部短链接排列组合
	 */
	@Insert("insert into short_url_info (Short_Url) values (#{shortUrl})")
	public void insertShortUrl(@Param("shortUrl") String shortUrl);
	
	
	/**
	 * 查询当前短链接是否被占用
	 */
	@Select("select Source_Url from short_url_info where Short_Url = #{shortUrl}")
	public String selectShortUrlIsBeUsed(@Param("shortUrl") String shortUrl);
	
	/**
	 * 插入短链接与源域名绑定信息
	 */
	@Update("update short_url_info set Source_Url = #{sourceUrl} ,Date = #{Date} where Short_Url = #{shortUrl}")
	public void updateSourceUrlWhithShortUrl(@Param("shortUrl") String shortUrl,
											 @Param("sourceUrl") String sourceUrl,
											 @Param("Date") String date);
	
	/**
	 * 查询一个未被占用的短链接
	 */
	@Select("select Short_Url from short_url_info where Source_Url is null limit 1")
	public String selectShortUrlNotUsed();
	
	/**
	 * 查询源URL是否存在
	 */
	@Select("select Source_Url from short_url_info where Source_Url = #{sourceUrl}")
	public String selectSourceUrl(@Param("sourceUrl") String sourceUrl);
	
	/**
	 * 查询短链接通过原链接
	 */
	@Select("select Short_Url from short_url_info where Source_Url = #{sourceUrl}")
	public String selectShortUrlBySourceUrl(@Param("sourceUrl") String sourceUrl);
	
	/**
	 * 查询原链接通过短链接
	 */
	@Select("select Source_Url from short_url_info where Short_Url = #{shortUrl}")
	public String selectSourceUrlByShortUrl(@Param("shortUrl") String shortUrl);
}
